package com.app.micaihu.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.app.micaihu.app.AppApplication;

import java.util.List;

/**
 * 所有adapter的基类
 *
 * @param <T>
 * @author linkai
 */
public abstract class AppBaseAdapter<T> extends BaseAdapter {
    public List<T> list;
    public Context context;
    public LayoutInflater inflater;

    public AppBaseAdapter(List<T> list, Context context) {
        super();
        this.list = list;
        this.context = context;
        if (this.context == null) {
            inflater = LayoutInflater.from(AppApplication.getInstance());
        } else {
            inflater = LayoutInflater.from(this.context);
        }

    }

    @Override
    public int getCount() {
        return list != null && !list.isEmpty() ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        if (position < 0) {
            position = 0;
        } else if (position >= list.size()) {
            position = list.size() - 1;
        }
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return creatView(position, convertView, parent);
    }

    /**
     * 重置数据集合
     *
     * @param list
     */
    public void resetList(List<T> list) {
        if (list != null) {
            list.clear();
        }
        list.addAll(list);
    }

    public abstract View creatView(int position, View convertView, ViewGroup parent);


}
